package org.bigdata.framework.admin.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bigdata.framework.core.model.CommunityCategory;
import org.bigdata.framework.core.model.CommunityDomain;
import org.bigdata.framework.core.model.CommunityKnowledge;
import org.bigdata.framework.core.model.CommunityKnowledgePoint;
import org.bigdata.framework.core.model.Menu;
import org.bigdata.framework.core.protocol.IBaseService;
import org.bigdata.framework.core.protocol.ICommunityService;
import org.bigdata.framework.core.protocol.IPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.xxl.conf.core.XxlConfClient;
/**
 * 知识社区
 * @author zf
 * 2017年9月13日09:46:06 
 */
@Controller
public class CommunityController extends BaseContoroller {
    @Autowired
    private IPublicService publicService;
    @Autowired
    private IBaseService baseService;
    @Autowired
    private ICommunityService communityService;
    
    //菜单---》领域管理
    @RequestMapping("community-domain-list.html")
    public String toCommunityDomainList() {
        return "/modules/community/community_domain_list";
    }
    //领域管理---》新增修改页
    @RequestMapping("community-domain-add-update.html")
    public String toCommunityDomainAddUpdate() {
        return "/modules/community/community_domain_add_update";
    }
    
    //菜单---》分类管理
    @RequestMapping("community-category-list.html")
    public String toCommunityCategoryList() {
        return "/modules/community/community_category_list";
    }
    //分类管理---》新增修改页
    @RequestMapping("community-category-add-update.html")
    public String toCommunityCategoryAddUpdate() {
        return "/modules/community/community_category_add_update";
    }
    //分类管理---》新增修改页---》所属领域
    @RequestMapping("community-category-domain.html")
    public String toCommunityCategoryDomain() {
        return "/modules/community/community_category_domain";
    }
    
    //菜单---》知识库管理
    @RequestMapping("community-knowledge-list.html")
    public String toCommunityKnowledgeList() {
        return "/modules/community/community_knowledge_list";
    }
    //知识库管理---》新增修改页
    @RequestMapping("community-knowledge-add-update.html")
    public String toCommunityKnowledgeAddUpdate() {
        return "/modules/community/community_knowledge_add_update";
    }
    //知识库管理---》新增修改页---》所属分类
    @RequestMapping("community-knowledge-category.html")
    public String toCommunityKnowledgeCategory() {
        return "/modules/community/community_knowledge_category";
    }
    //知识库管理---》添加知识点
    @RequestMapping("community-knowledge-point.html")
    public String toCommunityKnowledgePoint() {
    	return "/modules/community/community_knowledge_point";
    }
    /**
     * 领域管理
     * @param request
     * @param response
     * @throws Exception
     * @author zf
     * 2017年9月13日10:36:06
     */
    //分页查询--领域列表
    @RequestMapping("getCommunityDomainList")
    public void getCommunityDomainList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer pageSize = Integer.parseInt(request.getParameter("page"));
        Integer number = Integer.parseInt(request.getParameter("number"));
        String queryParam = request.getParameter("queryParams");
        String text = request.getParameter("text");
        //拼接-筛选条件
        HashMap<String, Object> queryParams = new HashMap<String, Object>();
        if (text != null && !"".equals(text)) {
            queryParams.put("%," + queryParam, text);
        }
        queryParams.put("^,sort", "asc");
        //得到结果集
        List<CommunityDomain> communityDomainList = communityService.getCommunityDomain(queryParams, pageSize, number);
        Integer count = communityService.getCommunityDomainCount(queryParams);//得到总数
        LinkedHashMap<String, Object> r =new LinkedHashMap<String, Object>();
        r.put("data", communityDomainList);
        r.put("count", count);
        r.put("page", (int)Math.ceil(Double.valueOf(count)/number));//得到分页数
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    
    //领域---》修改查询单个
    @RequestMapping("findCommunityDomain")
    public void findCommunityDomain(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        CommunityDomain communityDomain = communityService.findCommunityDomain(id);
        String resultString = formatToJson(communityDomain);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    //领域---》新增或修改
    @RequestMapping("addOrUploadCommunityDomain")
    public void addOrUploadCommunityDomain(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        String nameZh = request.getParameter("nameZh");
        String nameEn = request.getParameter("nameEn");
        String sort = request.getParameter("sort");
        String description = request.getParameter("description");
        String status = request.getParameter("status");
        CommunityDomain communityDomain = new CommunityDomain();
        communityDomain.setNameZh(nameZh);
        communityDomain.setNameEn(nameEn);
        communityDomain.setDescription(description);
        communityDomain.setSort(Integer.parseInt(sort));
        communityDomain.setStatus(Integer.parseInt(status));
        LinkedHashMap<String, Object> r = new LinkedHashMap<String, Object>();
        try {
            if("0".equals(id)){
                communityService.saveCommunityDomain(communityDomain);
            }else{
                communityDomain.setId(Integer.parseInt(id));
                communityService.editCommunityDomain(communityDomain);
            }
            r.put("data", "yes");
        } catch (Exception e) {
            r.put("data", "no");
        }
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    //领域---》删除
    @RequestMapping("deldeteCommunityDomain")
    public void deldeteCommunityDomain(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String rData="yes";
        try {
            CommunityDomain communityDomain = communityService.findCommunityDomain(id);
            communityService.deleteCommunityDomain(communityDomain);
        } catch (Exception e) {
            rData = "no";
        }
        List<String> r = new ArrayList<String>();
        r.add(rData);
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    /**
     * 领域结束
     * 2017年9月13日14:03:23
     */
    
    /**
     * 分类管理
     * @param request
     * @param response
     * @throws Exception
     * @author zf
     * 2017年9月13日14:05:03
     */
    //分页查询--分类列表
    @RequestMapping("getCommunityCategoryList")
    public void getCommunityCategoryList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer pageSize = Integer.parseInt(request.getParameter("page"));
        Integer number = Integer.parseInt(request.getParameter("number"));
        String queryParam = request.getParameter("queryParams");
        String text = request.getParameter("text");
        //拼接-筛选条件
        HashMap<String, Object> queryParams = new HashMap<String, Object>();
        if (text != null && !"".equals(text)) {
            queryParams.put("%," + queryParam, text);
        }
        queryParams.put("^,sort", "asc");
        //得到结果集
        List<CommunityCategory> communityCategoryList = communityService.getCommunityCategory(queryParams, pageSize, number);
        List<LinkedHashMap<String, Object>> data = new ArrayList<LinkedHashMap<String,Object>>();
        LinkedHashMap<String, Object> map = null;
        for (int i = 0; i < communityCategoryList.size(); i++) {
            map = new LinkedHashMap<String, Object>();
            CommunityCategory communityCategory = communityCategoryList.get(i);
            map.put("nameZh", communityCategory.getNameZh());
            map.put("nameEn", communityCategory.getNameEn());
            map.put("description", communityCategory.getDescription());
            map.put("id", communityCategory.getId());
            map.put("sort", communityCategory.getSort());
            map.put("status", communityCategory.getStatus());
            map.put("domainId", communityCategory.getDomainId());
            map.put("domainName", communityService.findCommunityDomain(communityCategory.getDomainId()).getNameZh());
            data.add(map);
        }
        Integer count = communityService.getCommunityCategoryCount(queryParams);//得到总数
        LinkedHashMap<String, Object> r =new LinkedHashMap<String, Object>();
        r.put("data", data);
        r.put("count", count);
        r.put("page", (int)Math.ceil(Double.valueOf(count)/number));//得到分页数
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    
    //分类---》修改查询单个
    @RequestMapping("findCommunityCategory")
    public void findCommunityCategory(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        CommunityCategory communityCategory = communityService.findCommunityCategory(id);
        LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("nameZh", communityCategory.getNameZh());
        map.put("nameEn", communityCategory.getNameEn());
        map.put("description", communityCategory.getDescription());
        map.put("id", communityCategory.getId());
        map.put("sort", communityCategory.getSort());
        map.put("status", communityCategory.getStatus());
        map.put("domainId", communityCategory.getDomainId());
        map.put("domainName", communityService.findCommunityDomain(communityCategory.getDomainId()).getNameZh());
        String resultString = formatToJson(map);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    //分类---》新增或修改
    @RequestMapping("addOrUploadCommunityCategory")
    public void addOrUploadCommunityCategory(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        String nameZh = request.getParameter("nameZh");
        String nameEn = request.getParameter("nameEn");
        String sort = request.getParameter("sort");
        String description = request.getParameter("description");
        String status = request.getParameter("status");
        String domainId = request.getParameter("domainId");
        CommunityCategory communityCategory = new CommunityCategory();
        communityCategory.setNameZh(nameZh);
        communityCategory.setNameEn(nameEn);
        communityCategory.setDescription(description);
        communityCategory.setSort(Integer.parseInt(sort));
        communityCategory.setStatus(Integer.parseInt(status));
        communityCategory.setDomainId(Integer.parseInt(domainId));
        LinkedHashMap<String, Object> r = new LinkedHashMap<String, Object>();
        try {
            if("0".equals(id)){
                communityService.saveCommunityCategory(communityCategory);
            }else{
                communityCategory.setId(Integer.parseInt(id));
                communityService.editCommunityCategory(communityCategory);
            }
            r.put("data", "yes");
        } catch (Exception e) {
            r.put("data", "no");
        }
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    //分类---》删除
    @RequestMapping("deldeteCommunityCategory")
    public void deldeteCommunityCategory(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String rData="yes";
        try {
            CommunityCategory communityCategory = communityService.findCommunityCategory(id);
            communityService.deleteCommunityCategory(communityCategory);
        } catch (Exception e) {
            rData = "no";
        }
        List<String> r = new ArrayList<String>();
        r.add(rData);
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    /**
     * 分类结束
     * 2017年9月13日16:32:04
     */
    
    /**
     * 知识库管理
     * @param request
     * @param response
     * @throws Exception
     * @author zf
     * 2017年9月13日16:32:10
     */
    //分页查询--知识库列表
    @RequestMapping("getCommunityKnowledgeList")
    public void getCommunityKnowledgeList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer pageSize = Integer.parseInt(request.getParameter("page"));
        String queryParam = request.getParameter("queryParams");
        String text = request.getParameter("text");
        //获取服务器地址
        String uploadPath =  XxlConfClient.get("dal.file.server", "");
        //拼接-筛选条件
        HashMap<String, Object> queryParams = new HashMap<String, Object>();
        if (text != null && !"".equals(text)) {
            queryParams.put("%," + queryParam, text);
        }
        queryParams.put("^,sort", "asc");
        //得到结果集
        List<CommunityKnowledge> communityKnowledgeList = communityService.getCommunityKnowledge(queryParams, pageSize, 15);
        List<LinkedHashMap<String, Object>> data = new ArrayList<LinkedHashMap<String,Object>>();
        LinkedHashMap<String, Object> map = null;
        for (int i = 0; i < communityKnowledgeList.size(); i++) {
            map = new LinkedHashMap<String, Object>();
            CommunityKnowledge communityKnowledge = communityKnowledgeList.get(i);
            map.put("nameZh", communityKnowledge.getNameZh());
            map.put("nameEn", communityKnowledge.getNameEn());
            map.put("description", communityKnowledge.getDescription());
            map.put("id", communityKnowledge.getId());
            map.put("sort", communityKnowledge.getSort());
            map.put("status", communityKnowledge.getStatus());
            map.put("categoryId", communityKnowledge.getCategoryId());
            map.put("recommend", communityKnowledge.getRecommend());
            if(null != communityKnowledge.getCover() && !"".equals(communityKnowledge.getCover())){
                map.put("cover", "<img src='"+uploadPath+communityKnowledge.getCover()+"' width='30' height='30' />");
            }else{
                map.put("cover", "");
            }
            map.put("categoryName", communityService.findCommunityCategory(communityKnowledge.getCategoryId()).getNameZh());
            data.add(map);
        }
        Integer count = communityService.getCommunityKnowledgeCount(queryParams);//得到总数
        LinkedHashMap<String, Object> r =new LinkedHashMap<String, Object>();
        r.put("data", data);
        r.put("count", count);
        r.put("page", (int)Math.ceil(Double.valueOf(count)/15));//得到分页数
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    
    //知识库---》修改查询单个
    @RequestMapping("findCommunityKnowledge")
    public void findCommunityKnowledge(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        //获取服务器地址
        String uploadPath =  XxlConfClient.get("dal.file.server", "");
        CommunityKnowledge communityKnowledge = communityService.findCommunityKnowledge(id);
        LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("nameZh", communityKnowledge.getNameZh());
        map.put("nameEn", communityKnowledge.getNameEn());
        map.put("description", communityKnowledge.getDescription());
        map.put("id", communityKnowledge.getId());
        map.put("sort", communityKnowledge.getSort());
        map.put("status", communityKnowledge.getStatus());
        map.put("categoryId", communityKnowledge.getCategoryId());
        map.put("categoryName", communityService.findCommunityCategory(communityKnowledge.getCategoryId()).getNameZh());
        map.put("recommend", communityKnowledge.getRecommend());
        map.put("cover", communityKnowledge.getCover());
        if(null != communityKnowledge.getCover() && !"".equals(communityKnowledge.getCover())){
            map.put("imges", "<img src='"+uploadPath+communityKnowledge.getCover()+"' width='30' height='30' />");
        }else{
            map.put("imges", "");
        }
        String resultString = formatToJson(map);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    //知识库---》新增或修改
    @RequestMapping("addOrUploadCommunityKnowledge")
    public void addOrUploadCommunityKnowledge(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        String nameZh = request.getParameter("nameZh");
        String nameEn = request.getParameter("nameEn");
        String sort = request.getParameter("sort");
        String description = request.getParameter("description");
        String status = request.getParameter("status");
        String categoryId = request.getParameter("categoryId");
        String cover = request.getParameter("cover");
        String recommend = request.getParameter("recommend");
        //获取服务器地址
        String uploadPath =  XxlConfClient.get("dal.file.server", "") +"/";
        CommunityKnowledge communityKnowledge = new CommunityKnowledge();
        communityKnowledge.setNameZh(nameZh);
        communityKnowledge.setNameEn(nameEn);
        communityKnowledge.setDescription(description);
        communityKnowledge.setSort(Integer.parseInt(sort));
        communityKnowledge.setStatus(Integer.parseInt(status));
        communityKnowledge.setCategoryId(Integer.parseInt(categoryId));
        communityKnowledge.setRecommend(Integer.parseInt(recommend));
        if(null != cover && !"".equals(cover)){
            communityKnowledge.setCover(cover.replaceAll(uploadPath, ""));
        }
        LinkedHashMap<String, Object> r = new LinkedHashMap<String, Object>();
        try {
            if("0".equals(id)){
                communityService.saveCommunityKnowledge(communityKnowledge);
            }else{
                communityKnowledge.setId(Integer.parseInt(id));
                communityService.editCommunityKnowledge(communityKnowledge);
            }
            r.put("data", "yes");
        } catch (Exception e) {
            r.put("data", "no");
        }
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    //知识库---》删除
    @RequestMapping("deldeteCommunityKnowledge")
    public void deldeteCommunityKnowledge(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String rData="yes";
        try {
            CommunityKnowledge communityKnowledge = communityService.findCommunityKnowledge(id);
            communityService.deleteCommunityKnowledge(communityKnowledge);
        } catch (Exception e) {
            rData = "no";
        }
        List<String> r = new ArrayList<String>();
        r.add(rData);
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    /**
     * 知识库结束
     * 2017年9月13日17:40:43
     */
   
    /**
     * 知识库知识点管理
     * @param request
     * @param response
     * @throws Exception
     * @author zf
     * 2017年9月13日16:32:10
     */
  //知识库管理---》添加知识点
    @RequestMapping("community-knowledge-point-addUpload.html")
    public ModelAndView toCommunityKnowledgePointAddUpload(HttpServletRequest request) {
    	String id = request.getParameter("id");
    	HashMap<String, Object> queryParams = new HashMap<String, Object>();
    	queryParams.put("=,knowledge_id ", id);
    	queryParams.put("=,parent_id ", 0);
    	List<CommunityKnowledgePoint> communityKnowledgePointList = communityService.getCommunityKnowledgePoint(queryParams, 1, communityService.getCommunityKnowledgePointCount(queryParams));
    	ModelAndView view = new ModelAndView();
		view.addObject("communityKnowledgePointList", communityKnowledgePointList);
		view.setViewName("/modules/community/community_knowledge_point_addUpload");
		return view;
    }
    //知识库知识点列表
    @RequestMapping("getCommunityKnowledgePointList")
    public void getCommunityKnowledgePointList(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        HashMap<String, Object> queryParams = new HashMap<String, Object>();
        queryParams.put("=,knowledge_id ", id);
        List<CommunityKnowledgePoint> communityKnowledgePointList = communityService.getCommunityKnowledgePoint(queryParams, 1, communityService.getCommunityKnowledgePointCount(queryParams));
        List<HashMap<String, Object>> r = new ArrayList<HashMap<String, Object>>();
		LinkedHashMap<String, Object> mp = null;
		for (int i = 0; i < communityKnowledgePointList.size(); i++) {
			CommunityKnowledgePoint communityKnowledgePoint = communityKnowledgePointList.get(i);
				mp = new LinkedHashMap<String, Object>();
				mp.put("id", communityKnowledgePoint.getId());
				mp.put("name", communityKnowledgePoint.getNameZh());
				mp.put("pId", communityKnowledgePoint.getParentId());
				r.add(mp);
		};
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    
    //知识库---》修改查询单个
    @RequestMapping("findCommunityKnowledgePoint")
    public void findCommunityKnowledgePoint(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        CommunityKnowledgePoint communityKnowledgePoint = communityService.findCommunityKnowledgePoint(id);
        LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("nameZh", communityKnowledgePoint.getNameZh());
        map.put("nameEn", communityKnowledgePoint.getNameEn());
        map.put("description", communityKnowledgePoint.getDescription());
        map.put("id", communityKnowledgePoint.getId());
        map.put("sort", communityKnowledgePoint.getSort());
        map.put("status", communityKnowledgePoint.getStatus());
        map.put("knowledgeId", communityKnowledgePoint.getKnowledgeId());
        map.put("parentId", communityKnowledgePoint.getParentId());
        String resultString = formatToJson(map);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    //知识库---》新增或修改
    @RequestMapping("addOrUploadCommunityKnowledgePoint")
    public void addOrUploadCommunityKnowledgePoint(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        String knowledgeId = request.getParameter("knowledgeId");
        String nameZh = request.getParameter("nameZh");
        String nameEn = request.getParameter("nameEn");
        String parentId = request.getParameter("parentId");
        String sort = request.getParameter("sort");
        String description = request.getParameter("description");
        String status = request.getParameter("status");
      
       
        CommunityKnowledgePoint communityKnowledgePoint = new CommunityKnowledgePoint();
        communityKnowledgePoint.setKnowledgeId(Integer.parseInt(knowledgeId));
        communityKnowledgePoint.setNameZh(nameZh);
        communityKnowledgePoint.setNameEn(nameEn);
        communityKnowledgePoint.setParentId(Integer.parseInt(parentId));
        communityKnowledgePoint.setSort(Integer.parseInt(sort));
        communityKnowledgePoint.setStatus(Integer.parseInt(status));
        communityKnowledgePoint.setDescription(description);
        LinkedHashMap<String, Object> r = new LinkedHashMap<String, Object>();
        try {
            if(null != id && !"".equals(id)){
            	communityKnowledgePoint.setId(Integer.parseInt(id));
            	communityService.editCommunityKnowledgePoint(communityKnowledgePoint);
            }else{
            	communityService.saveCommunityKnowledgePoint(communityKnowledgePoint);
            }
            r.put("data", "yes");
        } catch (Exception e) {
            r.put("data", "no");
        }
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    //知识库---》删除
    @RequestMapping("deldeteCommunityKnowledgePoint")
    public void deldeteCommunityKnowledgePoint(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        String rData="yes";
        try {
            CommunityKnowledgePoint communityKnowledgePoint = communityService.findCommunityKnowledgePoint(id);
            communityService.deleteCommunityKnowledgePoint(communityKnowledgePoint);
        } catch (Exception e) {
            rData = "no";
        }
        LinkedHashMap<String, Object> r = new LinkedHashMap<String, Object>();
        r.put("data", rData);
        String resultString = formatToJson(r);
        super.pringWriterToPage(resultString, "application/json", response);
    }
    /**
     * 知识库知识点结束
     * 2017年9月13日17:40:43
     */
    
}
